package com.open.capacity.user.service.impl;

import com.open.capacity.common.constant.UserType;
import com.open.capacity.common.model.SysRole;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.util.ValidatorUtil;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.dao.EducationalAdminDao;
import com.open.capacity.user.dao.SysUserDao;
import com.open.capacity.user.dao.SysUserRoleDao;
import com.open.capacity.user.service.EducationalAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cheng
 * @date 2021-02-02 11:18
 */
@Slf4j
@Service
public class EducationalAdminServiceImpl implements EducationalAdminService {
    @Autowired
    private EducationalAdminDao educationalAdminDao;
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public List<SysUser> findPersons(Map<String, Object> params) {
        return educationalAdminDao.findPersons(params);
    }

    @Override
    public List<SysRole> findRolesNoStudent(Map<String, Object> params) {
        return educationalAdminDao.findRolesNoStudent(params);
    }

    @Override
    public List<SysUser> findTeacher(Map<String, Object> params) {
        return educationalAdminDao.findTeacher(params);
    }

    @Override
    @Transactional
    public Result addStudent(SysUser sysUser) {
        String username = sysUser.getUsername();

        if (ValidatorUtil.checkPhone(username)) {// 防止用手机号直接当用户名，手机号要发短信验证
            return Result.failed("用户名要包含英文字符");
        }

        if (StringUtils.isBlank(username)) {
            return Result.failed("用户名不能为空");
        }

        if (username.contains("@")) {// 防止用邮箱直接当用户名，邮箱也要发送验证（暂未开发）
            return Result.failed("用户名不能包含@");
        }

        if (username.contains("|")) {
            return Result.failed("用户名不能包含|字符");
        }

        if (StringUtils.isBlank(sysUser.getType())) {
            sysUser.setType(UserType.BACKEND.name());
        }

        if (StringUtils.isBlank(sysUser.getNickname())) {
            sysUser.setNickname(username);
        }


        if (!StringUtils.isBlank(sysUser.getPhone())) {

            if (!ValidatorUtil.checkPhone(sysUser.getPhone())) {// 防止用手机号直接当用户名，手机号要发短信验证
                return Result.failed("手机号格式不正确");
            }

        }

        sysUser.setCreateTime(new Date());

        int i = 0;

        if (sysUser.getId() ==null){
            SysUser persistenceUser = sysUserDao.findByUsername(sysUser.getUsername());
            if (persistenceUser != null && persistenceUser.getUsername() != null) {
                return Result.failed("用户名已存在");
            }
            if (sysUserDao.findByEmail(sysUser) != null) {
                return Result.failed("邮箱已存在");
            }
            if (sysUserDao.findByPhone(sysUser) != null) {
                return Result.failed("电话已存在");
            }
            sysUser.setEnabled(Boolean.TRUE);
            sysUser.setPassword(passwordEncoder.encode("123456"));
            sysUser.setUpdateTime(sysUser.getCreateTime());
            i = sysUserDao.save(sysUser);
            educationalAdminDao.addStu_Teacher(sysUser);
            educationalAdminDao.addRoles(sysUser);
        }else {
            if (sysUserDao.findOnlyEmail(sysUser) != null) {
                return Result.failed("邮箱已被注册");
            }
            if (sysUserDao.findOnlyPhone(sysUser) != null) {
                return Result.failed("电话已被注册");
            }
            sysUser.setUpdateTime(new Date());
            i = sysUserDao.updateByPrimaryKey(sysUser);
            educationalAdminDao.updateTeacher(sysUser);
        }


        return i > 0 ? Result.succeed(sysUser, "操作成功") : Result.failed("操作失败");
    }

    @Override
    public int findPersonsCount(Map<String, Object> params) {
        return educationalAdminDao.findPersonsCount(params);
    }
}
